<?php
/**
 * 供应商信息
 * PHP version 7
 *
 * @address   Pay
 * @package   App\Http\Controllers\Pay
 * @author    zhengys <zhengys@boqii.net>
 * @copyright 2016-2019 guangcheng Co. All Rights Reserved.
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 * @version   GIT:<git_id>
 * @link      http://shop.openapi.boqii.com
 */

namespace App\Http\Controllers\Supplier;

use Log;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use App\Models\Supplier\Supplier;
use App\Models\Supplier\SupplierMatorApply;

/**
 * 供应商信息
 * Class Login
 *
 * @package App\Http\Controllers\Supplier
 */
class SupplierController extends Controller
{
	/**
     * @var \App\Models\Supplier\Supplier
    */
    private $model;
	
	/**
     * @var \App\Models\Supplier\Supplier
    */
    private $mator_apply;
	
	/**
     * LoginController constructor.
     *
     * @param \App\Models\Common\Login
     */
    public function __construct(Supplier $supplier)
    {
        $this->model = $supplier;
		$this->mator_apply = new SupplierMatorApply();
    }
	
	/**
	 * 供应商信息
	 *
	 * @param Request $request
     * @param int     $id
     * @param string  $version
     * @access public
     * @return mixed
     */
	public function view(Request $request, int $id, string $version = '1.0') : Collection
    {
		$messages = [
			'required' => ':attribute 的字段是必要的'
		];
		$rules = [
			'SupplierId' => $request->input('SupplierId'),
			'SupplierId' => 'required'
		];
		$validator = Validator::make($request->input(),$rules,$messages);
        if ($validator->fails()) 
		{
            $this->throwValidationException($request, $validator);
        }
		$supplier_id = authcode($request->input('SupplierId'));
		$supplier_obj = Supplier::find($supplier_id);
		if($supplier_obj == NULL){
			throw new \Exception('供应商参数有误!');
		}
		$result = array();
		$result['SupplierId'] = $request->input('SupplierId');
		$result['SupplierMobile'] = $supplier_obj->supplier_mobile;
		$result['SupplierPass'] = $supplier_obj->supplier_pass;
		$result['ContactName'] = $supplier_obj->contact_name;
		$result['ContactMobile'] = $supplier_obj->contact_mobile;
		$result['CompName'] = $supplier_obj->comp_name;
		$result['CompType'] = $supplier_obj->comp_type;
		$result['ProId'] = $supplier_obj->pro_id;
		$result['CityId'] = $supplier_obj->city_id;
		$result['DistrictId'] = $supplier_obj->district_id;
		$result['Address'] = $supplier_obj->address;
        return collect(['List'=>$result]);
	}
	
	/**
	 * 更新供应商
	 *
	 * @param Request $request
     * @param int     $id
     * @param string  $version
     * @access public
     * @return mixed
     */
	public function save(Request $request, int $id, string $version = '1.0') : Collection
    {
		$messages = [
			'required' => ':attribute 的字段是必要的'
		];
		$rules = [
			'SupplierId' => $request->input('SupplierId'),
			'SupplierId' => 'required'
		];
		$validator = Validator::make($request->input(),$rules,$messages);
        if ($validator->fails()) 
		{
            $this->throwValidationException($request, $validator);
        }
		$supplier_id = authcode($request->input('SupplierId'));
		$supplier_obj = Supplier::find($supplier_id);
		if($supplier_obj == NULL){
			throw new \Exception('供应商参数有误!');
		}
		$where = array();
		$where['id'] = $supplier_id;
		$update_data = array();
		$update_data['supplier_mobile'] = $request->input('SupplierMobile');
		$update_data['supplier_pass'] = $request->input('SupplierPass');
		$update_data['contact_name'] = $request->input('ContactName');
		$update_data['contact_mobile'] = $request->input('ContactMobile');
		$update_data['comp_name'] = $request->input('CompName');
		$update_data['comp_type'] = $request->input('CompType');
		$update_data['pro_id'] = $request->input('ProId');
		$update_data['city_id'] = $request->input('CityId');
		$update_data['district_id'] = $request->input('DistrictId');
		$update_data['address'] = $request->input('Address');
		if($supplier_id > 0){
			Supplier::updateOrCreate($update_data, $where);
		}else{
			Supplier::create($update_data);
		}
        return collect();
	}
	
	/**
	 * 供应车型库申请接口
	 *
	 * @param Request $request
     * @param int     $id
     * @param string  $version
     * @access public
     * @return mixed
     */
	public function mapply(Request $request, int $id, string $version = '1.0') : Collection
    {
		$messages = [
			'required' => ':attribute 的字段是必要的'
		];
		$rules = [
			'SupplierId' => $request->input('SupplierId'),
			'SupplierId' => 'required'
		];
		$validator = Validator::make($request->input(),$rules,$messages);
        if ($validator->fails()) 
		{
            $this->throwValidationException($request, $validator);
        }
		//$supplier_id = authcode($request->input('SupplierId'));
		$supplier_id = $request->input('SupplierId');
		$supplier_obj = Supplier::find($supplier_id);
		if($supplier_obj == NULL){
			throw new \Exception('供应商参数有误!');
		}
		$apply_data = array();
		$apply_data['supplier_id'] = $supplier_id;
		$apply_data['f_c_id'] = $request->input('FieldCountryId');
		$apply_data['make_id'] = $request->input('MakeId');
		$apply_data['series_id'] = $request->input('SeriesId');
		$apply_data['mator_code'] = $request->input('MatorCode');
		$apply_data['mator_name'] = $request->input('MatorName');
		$apply_data['engine'] = $request->input('Engine');
		$apply_data['volume'] = $request->input('Volume');
		$apply_data['capacity'] = $request->input('Capacity');
		$apply_data['horse_power'] = $request->input('HorsePower');
		$apply_data['m_type'] = $request->input(Mtype);
		$apply_data['m_year'] = $request->input('Myear');
		$apply_data['create_time'] = time();
		SupplierMatorApply::create($apply_data);
		return collect();
	}
	
	/**
	 * 供应车型库审核接口
	 *
	 * @param Request $request
     * @param int     $id
     * @param string  $version
     * @access public
     * @return mixed
     */
	public function mpass(Request $request, int $id, string $version = '1.0') : Collection
    {
		$messages = [
			'required' => ':attribute 的字段是必要的'
		];
		$rules = [
			'SupplierId' => $request->input('SupplierId'),
			'SupplierId' => 'required'
		];
		$validator = Validator::make($request->input(),$rules,$messages);
        if ($validator->fails()) 
		{
            $this->throwValidationException($request, $validator);
        }
		$supplier_id = authcode($request->input('SupplierId'));
		$supplier_obj = Supplier::find($supplier_id);
		if($supplier_obj == NULL){
			throw new \Exception('供应商参数有误!');
		}
		$ApplyId = $request->input('ApplyId');
		Supplier::updateOrCreate(['check_status'=>2], ['id'=>$ApplyId]);
        return collect();
	}
	
	/**
	 * 供应车型库申请列表
	 *
	 * @param Request $request
     * @param int     $id
     * @param string  $version
     * @access public
     * @return mixed
     */
	public function mlist(Request $request, int $id, string $version = '1.0') : Collection
    {
		$params = $request->all();
		if(!empty($request->input('SupplierId'))){
			//$supplier_id = authcode($request->input('SupplierId'));
			$supplier_id = $request->input('SupplierId');
			$supplier_obj = Supplier::find($supplier_id);
			if($supplier_obj == NULL){
				throw new \Exception('供应商参数有误!');
			}
			$params['SupplierId'] = $supplier_id;
		}
		$result = $this->mator_apply->getSupplierApplyList($params);
        return collect(['List'=>$ret]);
	}
	
	
}